package com.situ.blog.controller;



import com.situ.blog.pojo.entity.Blog;
import com.situ.blog.pojo.entity.BlogType;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.service.IBlogService;
import com.situ.blog.service.IBlogTypeService;
import com.situ.blog.service.impl.BlogServiceImpl;
import com.situ.blog.service.impl.BlogTypeServiceImpl;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.LayUITableResult;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    private IBlogService blogService = new BlogServiceImpl();
    private IBlogTypeService blogTypeService = new BlogTypeServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "getBlogListPage":
                getBlogListPage(req, resp);
                break;
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
            case "getBlogAddPage":
                getBlogAddPage(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "updateStatus":
                updateStatus(req, resp);
                break;
        }
    }

    private void getBlogListPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/views/blog/blog_list.jsp").forward(req, resp);
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        blogService.updateStatus(Integer.parseInt(id), Integer.parseInt(status));

        JSONUtil.toJSON(resp, Result.ok("更新状态成功"));
    }

    private void getBlogAddPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<BlogType> list = blogTypeService.selectAll();
        req.setAttribute("list", list);
        req.getRequestDispatcher("/WEB-INF/views/blog/blog_add.jsp").forward(req, resp);
    }


    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BlogServlet.add");
        String title = req.getParameter("title");
        String image = req.getParameter("image");
        String content = req.getParameter("content");
        String typeId = req.getParameter("typeId");
        Blog blog = new Blog(title, image, content, Integer.parseInt(typeId));
        blogService.add(blog);
        //resp.sendRedirect(req.getContextPath() + "/blog");

        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BlogServlet.deleteById");
        String id = req.getParameter("id");
        blogService.deleteById(Integer.parseInt(id));
        //resp.sendRedirect(req.getContextPath() + "/blog");

        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BlogServlet.deleteAll");
        //String id = req.getParameter("id");
        String[] ids = req.getParameterValues("ids[]");
        blogService.deleteAll(ids);

        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    //http://localhost:8080/JavaWeb/blog?method=selectByPage&page=1&limit=10
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BlogServlet.selectByPage");
        String pageStr = req.getParameter("page");
        if (pageStr == null || pageStr.equals("")) {
            pageStr = "1";
        }
        String limitStr = req.getParameter("limit");
        if (limitStr == null || limitStr.equals("")) {
            limitStr = "10";
        }
        int page = Integer.parseInt(pageStr);
        int limit = Integer.parseInt(limitStr);
        String name = req.getParameter("name");
        BlogQuery blogQuery = new BlogQuery(page, limit, name);

        LayUITableResult layUITableResult = blogService.selectByPage(blogQuery);
        System.out.println("LayUITableResult: " + layUITableResult);

        JSONUtil.toJSON(resp, layUITableResult);
    }

}
